package com.xindaoapp.happypet.viewpager.windows;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.application.HappyPetApplication;
import com.xindaoapp.happypet.bean.Message;
import com.xindaoapp.happypet.utils.CommonParameter.UserState;
import com.xindaoapp.happypet.utils.Constants;
import com.xindaoapp.happypet.utils.IRequest;
import com.xindaoapp.happypet.viewpager.ViewPagerWindow;
import com.xindaoapp.happypet.viewpager.adapter.MyPostAdapter;

/**
 * 引用帖子之我的帖子
 * 
 * @author xindaoapp
 * 
 */
public class QuotePostByMyPostWindow extends ViewPagerWindow {

    public QuotePostByMyPostWindow(Activity context) {
        super(context);
    }

    @Override
    protected View onCreateView() {
        return View.inflate(mContext, R.layout.layout_listview, null);
    }

    private PullToRefreshListView vPullToRefreshListView;
    private View vLoadingView;

    @Override
    protected void initViews() {
        super.initViews();
        vLoadingView = getView().findViewById(R.id.loading);
        vPullToRefreshListView = (PullToRefreshListView) getView().findViewById(R.id.listview);
        vPullToRefreshListView.setOnRefreshListener(mOnRefreshListener);
        vPullToRefreshListView.setOnItemClickListener(mOnItemClickListener);
    }

    private final OnItemClickListener mOnItemClickListener = new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Message info = (Message) parent.getAdapter().getItem(position);
            Intent data = new Intent();
            data.putExtra(Constants.KEY_POST_NAME, info.subject);
            data.putExtra(Constants.KEY_POST_TID, info.tid);
            if (info.pics.size() > 0) {
                data.putExtra(Constants.KEY_POST_COVER, info.pics.get(0).url);
            }
            mContext.setResult(Activity.RESULT_OK, data);
            mContext.finish();
        }
    };
    private final OnRefreshListener<ListView> mOnRefreshListener =
            new OnRefreshListener<ListView>() {

                @Override
                public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                    onLoadDatas();
                }
            };

    @Override
    protected void onLoadDatas() {
        HappyPetApplication
                .get()
                .getMoccaApi()
                .getMyPost(UserState.getUserUid(), Constants.PAGE_FIRST, Constants.PAGE_SIZE,
                        new IRequest<List<Message>>() {

                            @Override
                            public void request(List<Message> t) {
                                if (null != t) {
                                    vPullToRefreshListView.setAdapter(new MyPostAdapter(
                                            getBaseContext(), t, Constants.PAGE_SIZE,
                                            R.layout.menu_message_item, R.layout.item_loading, 0,
                                            false));
                                }
                                vLoadingView.setVisibility(View.GONE);
                                vPullToRefreshListView.onRefreshComplete();
                            }
                        });
    }

}
